package com.xingkeduo.remote;

import com.xingkeduo.dto.transter.comment.CommentSubmit;
import com.xingkeduo.pojo.enums.CommentStar;
import com.xingkeduo.pojo.table.Comments;
import com.xingkeduo.pojo.table.Labels;
import com.xingkeduo.remote.feign.CommentsFeignService;
import com.xingkeduo.utils.ResponseUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class CommentRemoteService {

    @Resource
    private CommentsFeignService commentsFeignService;


    public Map<Integer, Object> queryLabelsByStart(){
        Map<Integer, Object> conMap = new HashMap<>();
        labels(conMap,4);
        labels(conMap,5);
        return conMap;
    }

    public void labels(Map<Integer, Object> conMap,int num){
        ResponseUtils<List<Labels>> responseUtils=  commentsFeignService.queryLabelsByStart(num);
        if(responseUtils.success()){
            List<Labels> data = responseUtils.getData();
            List<String> collect= data.stream().map(Labels::getLabelName).collect(Collectors.toList());
            conMap.put(num,collect);
        }
    }

    /**
     * 根据星级获取对应标签
     * @param num
     * @return
     */
    public Map<String,Long> getLabelIds(int num){
        ResponseUtils<List<Labels>> responseUtils=  commentsFeignService.queryLabelsByStart(num);
        if(responseUtils.success()){
            List<Labels> data = responseUtils.getData();
            return data.stream().collect(Collectors.toMap(Labels::getLabelName,Labels::getId));

        }
        return new HashMap<>();
    }


    /**
     * 获取所有标签
     * @return
     */
    public Map<String,Object> getLabes(){
        ResponseUtils<List<Labels>> responseUtils=  commentsFeignService.queryLabelsByStart(-1);
        Map<String,Object> map = new HashMap<>();
        if(responseUtils.success()){
            List<Labels> data = responseUtils.getData();
            Set<Integer> startNums = data.stream()
                    .map(Labels::getStartNum)
                    .collect(Collectors.toSet());

            for (Integer i:startNums) {
                map.put(i+"",getLabelNames(i));
            }
        }
        return map;
    }



    public Map<String,Object> getLabelNames(int num){
        Map<String,Object> map = new HashMap<>();
        ResponseUtils<List<Labels>> responseUtils=  commentsFeignService.queryLabelsByStart(num);
        if(responseUtils.success()){
            List<Labels> data = responseUtils.getData();
            List<String> list = data.stream().map(Labels::getLabelName).collect(Collectors.toList());
            map.put("tag",list);
            map.put("comment", CommentStar.getValue(num));

        }
        return map;
    }




    /**
     * 获取评论信息
     * @param orderNo
     * @return
     */
    public CommentSubmit queryDetailByorderId(String orderNo){
        ResponseUtils<Comments> commentsResponseUtils = commentsFeignService.queryDetailByorderNo(orderNo);
        if(commentsResponseUtils.getData()!=null){
            CommentSubmit commentSubmit = new CommentSubmit();
            commentSubmit.setError(8);
            commentSubmit.setReason("此订单已经评价过，无法再次评价！");
            return commentSubmit;
        }
        return null;
    }

}
